package com.project.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.project.dao.implement.AccountTreeMenuImpl;

public class AccountTreeMenuServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AccountTreeMenuServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        AccountTreeMenuImpl menuMapper = new AccountTreeMenuImpl();
        
        List<String> menuList = menuMapper.GetBankNameList();
        
        JSONObject json = new JSONObject(); 
    	try{    
    		JSONArray jsonArray =new JSONArray();
    		Set<String> set = new HashSet();
    		Iterator i = menuList.iterator();
    		
    		String outBankName = "";
    		boolean isHasOtherBank = false;
    		while(i.hasNext()){  
    			outBankName = (String)i.next();
    			if (outBankName.indexOf("招") != -1)
    			{
    				outBankName = "招商银行";
    			}
    			else if (outBankName.indexOf("交") != -1)
    			{
    				outBankName = "交通银行";
    			}
    			else if (outBankName.indexOf("中国银行") != -1)
    			{
    				outBankName = "中国银行";
    			}
    			else if (outBankName.indexOf("工") != -1)
    			{
    				outBankName = "工商银行";
    			}
    			else if (outBankName.indexOf("广发") != -1
    					|| outBankName.indexOf("广东发展") != -1)
    			{
    				outBankName = "广发银行";
    			}
    			else if (outBankName.indexOf("浦发") != -1
    					|| outBankName.indexOf("浦东发展") != -1)
    			{
    				outBankName = "浦发银行";
    			}
    			else if (outBankName.indexOf("华夏银行") != -1)
    			{
    				outBankName = "华夏银行";
    			}
    			else if (outBankName.indexOf("农业银行") != -1
    					|| outBankName.indexOf("农行") != -1)
    			{
    				outBankName = "农业银行";
    			}
    			else if (outBankName.indexOf("民生") != -1)
    			{
    				outBankName = "民生银行";
    			}
    			else if (outBankName.indexOf("光大") != -1)
    			{
    				outBankName = "光大银行";
    			}
    			else if (outBankName.indexOf("汉口银行") != -1)
    			{
    				outBankName = "汉口银行";
    			}
    			else if (outBankName.indexOf("长沙") != -1)
    			{
    				outBankName = "长沙银行";
    			}
    			else if (outBankName.indexOf("深发") != -1
    					|| outBankName.indexOf("深圳发展") != -1)
    			{
    				outBankName = "深圳银行";
    			}
    			else if (outBankName.indexOf("农村信用") != -1)
    			{
    				outBankName = "农村信用合作社";
    			}
    			else if (outBankName.indexOf("兴业") != -1)
    			{
    				outBankName = "兴业银行";
    			}
    			else
    			{
    				isHasOtherBank = true;
    				continue;
    			}
    			
    			set.add(outBankName);
            } 
    		if (isHasOtherBank)
    		{
    			set.add("其他银行");
    		}
    		Iterator setIt = set.iterator();
    		while(setIt.hasNext())
    		{
    			jsonArray.put(setIt.next());  
    		}
    		json.put("jsonArray", jsonArray);
    	}
    	catch(Exception e){               
    		e.printStackTrace();           
    	}           
    	        
    	response.getWriter().write(json.toString());  
    	response.getWriter().close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
